package com.meilai.project.controller.web.personnel.org;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.personnel.org.RoleDTO;
import com.meilai.project.service.system.MiniRightService;
import com.meilai.project.service.personnel.org.RoleService;
import com.meilai.project.service.system.RouterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "web端角色控制器", description = "web端角色")
@RestController
@RequestMapping("api/web/role")
public class WebRoleController {

    @Autowired
    private RoleService service;

    @Autowired
    private RouterService routerService;

    @Autowired
    private MiniRightService miniRightService;

    @GetMapping("list")
    @ApiOperation(value = "列表")
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size",defaultValue = "10") Integer page_size,
                             @RequestParam(name = "name", required = false) String name,
                             @RequestParam(name = "status", required = false) Integer status) {
        return new CommonResult().success(service.selectPageList(page, page_size, name, status));
    }

    @PutMapping("status")
    @ApiOperation(value = "更改状态")
    public CommonResult changeStatus(@RequestParam(value = "ids[]") Long ids, Integer status) {
        if (service.changeStatus(ids, status)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("delete")
    @ApiOperation(value = "删除")
    public CommonResult deleted(@RequestParam(value = "ids[]") List<Long> ids) {
        if (service.del(ids)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    public CommonResult add(@RequestBody RoleDTO dto) {
        int row = service.addOne(dto);
        if (row == 1) {
            return new CommonResult().success("新增成功");
        } else {
            return new CommonResult().failed("新增失败");
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改")
    public CommonResult edit(@RequestBody RoleDTO dto) {
        int row = service.editOne(dto);
        if (row == 1) {
            return new CommonResult().success("修改成功");
        } else {
            return new CommonResult().failed("修改失败");
        }
    }

    @GetMapping("check_code_unique")
    @ApiOperation(value = "检查code是否已存在")
    public CommonResult rights(@RequestParam(name = "code", required = true) String code,
                               @RequestParam(name = "is_edit", required = false) Integer is_edit,
                               @RequestParam(name = "id", required = false) Long id){
        return new CommonResult().success(service.checkCodeUnique(code, is_edit == 1, id));
    }

    @GetMapping("detail/{id}")
    @ApiOperation(value = "详情")
    public CommonResult detail(@PathVariable("id") Long id){
        return new CommonResult().success(service.selectDetail(id));
    }

    @GetMapping("rights")
    @ApiOperation(value = "整个权限树")
    public CommonResult rights(){
        return new CommonResult().success(routerService.getRights());
    }

    @GetMapping("router_rights")
    @ApiOperation(value = "菜单树")
    public CommonResult routerRights(){
        return new CommonResult().success(routerService.getRouterRights());
    }

    @GetMapping("mini_rights")
    @ApiOperation(value = "小程序的整个权限树")
    public CommonResult miniRights(){
        return new CommonResult().success(miniRightService.getRights());
    }

    @GetMapping("mini_router_rights")
    @ApiOperation(value = "小程序的菜单树")
    public CommonResult miniRouterRights(){
        return new CommonResult().success(miniRightService.getRouterRights());
    }
}
